package com.itcast.mallapplet.controller.user;


import com.itcast.applet.dto.AddressDTO;
import com.itcast.applet.entity.Address;
import com.itcast.context.BaseContext;
import com.itcast.mallapplet.service.AddressService;
import com.itcast.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/address")
@Slf4j
@Api(tags = "用户端-地址管理")
public class AddressController {
    @Autowired
    private AddressService addressService;

    //接口已通过-OK
    @PostMapping("/add")
    @ApiOperation("新增地址")
    public Result addAddress(@RequestBody AddressDTO addressDTO){
        log.info("新增地址信息为:{}",addressDTO);
        //封装一个地址对象
        Address address = new Address();
        BeanUtils.copyProperties(addressDTO, address);
        address.setUserId(BaseContext.getCurrentId());
        addressService.addAddress(address);
        return Result.success();
    }

    @GetMapping("/query")
    @ApiOperation("查询单个用户地址")
    public Result<Address> queryAddress(Long id){
        log.info("查询单个地址的id为:{}",id);
        Address address = addressService.queryAddress(id);
        return Result.success(address);
    }

    @GetMapping("/queryAll")
    @ApiOperation("查询全部用户地址")
    public Result<List<Address>> queryAllAddress(){
        log.info("查询全部用户地址");
        List<Address> addressList = addressService.queryAllAddress();
        return Result.success(addressList);
    }

    @PostMapping("/amend")
    @ApiOperation("修改地址")
    public Result amendAddress(@RequestBody Address address){
        log.info("修改地址的信息:{}",address);
        addressService.amendAddress(address);
        return Result.success();
    }

    @PostMapping("/delete")
    @ApiOperation("批量删除地址")
    public Result deleteAddressAll(@RequestBody List<Long> ids){
        log.info("批量修改地址的信息:{}",ids);
        addressService.deleteAddressAll(ids);
        return Result.success();
    }

}
